package com.lanou.projecta_163news.fragment.mainAdapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.lanou.projecta_163news.R;

import java.util.List;

/**
 * Created by dllo on 16/3/9.
 */
public class TopicRecyclerAdapter extends RecyclerView.Adapter<TopicRecyclerAdapter.ToRvViewHolder> {
    private List<Topic> toData;
    private int position;
    private TopicRecyclerItemListener topicRecyclerItemListener;

    public void setToRvItemListener(TopicRecyclerItemListener topicRecyclerItemListener) {
        this.topicRecyclerItemListener = topicRecyclerItemListener;
    }

    public void addToData(List<Topic> toData) {
        this.toData = toData;
        notifyDataSetChanged();
    }

    @Override
    public ToRvViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.topic_rv_item, null);
        return new ToRvViewHolder(view);
    }

    @Override
    public void onBindViewHolder(ToRvViewHolder holder, int position) {
        if (toData != null && toData.size() > 0) {
            Topic topic = toData.get(position);
            holder.topicIv.setImageResource(topic.getTopicImageIv());
            holder.topicConTv.setText(topic.getTopicConTv());
            holder.topicHeadTv.setText(topic.getToipicHeadTv());

            holder.position = position;
        }
    }

    @Override
    public int getItemCount() {
        return toData != null && toData.size() > 0 ? toData.size() : 0;
    }

    public class ToRvViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        private ImageView topicIv, topicHeadIv;
        private TextView topicHeadTv, topicConTv, topicClassTv, topicattentionTv, topicstateTv;
        private RelativeLayout topicRL;
        private int position;

        public ToRvViewHolder(View itemView) {
            super(itemView);
            topicIv = (ImageView) itemView.findViewById(R.id.topic_rv_item_iv);
            topicHeadIv = (ImageView) itemView.findViewById(R.id.topic_rv_item_headiv);
            topicHeadTv = (TextView) itemView.findViewById(R.id.topic_rv_item_uershead_tv);
            topicConTv = (TextView) itemView.findViewById(R.id.topic_rv_content_tv);
            topicClassTv = (TextView) itemView.findViewById(R.id.topic_rv_b_class_tv);
            topicattentionTv = (TextView) itemView.findViewById(R.id.topic_rv_b_attention_tv);
            topicstateTv = (TextView) itemView.findViewById(R.id.topic_rv_b_state_tv);
            topicRL = (RelativeLayout) itemView.findViewById(R.id.topic_rv_relayout);
            topicRL.setOnClickListener(this);

        }

        @Override
        public void onClick(View v) {
            Topic t = toData.get(position);
            if (topicRecyclerItemListener != null){
                topicRecyclerItemListener.toRvItemListener(t);
            }

        }
    }
}
